<template>
    <view class="xilu">
        <mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
            <view class="container plr30 pt30">
                <view class="flex-box pb40" v-for="(item,index) in userLists">
                    <image :src="item.avatar" mode="aspectFill" class="xilu_avatar"></image>
                    <view class="flex-grow-1">
                        <view class="fs32 blue pb20">{{item.nickname}}</view>
                        <view class="fs24 col5">粉丝数：{{item.fans_count}}</view>
                    </view>
                    <view class="btn3" @click="toggleFocus(index,item.id)">已关注</view>
                </view>
            </view>
        </mescroll-body>
    </view>
</template>

<script>
    // 引入mescroll-mixins.js
    import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
    export default {
        mixins: [MescrollMixin], // 使用mixin
        data() {
            return {
				userLists:[]
            };
        },
        methods: {
			/*上拉加载的回调*/
			upCallback(page) {
				// 此处可以继续请求其他接口
				let pageNum = page.num; // 页码, 默认从1开始
				let pageSize = page.size; // 页长, 默认每页10条
				this.$core.get({
					url: 'xilumarket.forum/user_focus',
					loading:false,
					data:{page: pageNum,pageSize:pageSize},
					success: (ret) => {
						let data = ret.data;
						// 接口返回的当前页数据列表 (数组)
						let curPageData = data.data;
						// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
						let curPageLen = curPageData.length;
						// 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
						let totalPage = data.last_page;
				
						//设置列表数据
						if (page.num == 1) this.userLists = []; //如果是第一页需手动置空列表
						this.userLists = this.userLists.concat(curPageData); //追加新数据
				
						// 请求成功,隐藏加载状态
						//方法一(推荐): 后台接口有返回列表的总页数 totalPage
						this.mescroll.endByPage(curPageLen, totalPage);
				
						
						setTimeout(() => {
							this.mescroll.endSuccess(curPageLen)
						}, 20)
					},
					fail: () => {
						//  请求失败,隐藏加载状态
						this.mescroll.endErr()
					}
				})
					
			},
					
			// 关注取关
			toggleFocus(index,userId) {
				if(!this.$core.getUserinfo(true)){
					return true;
				}
				let page = this;
				uni.showModal({
					title:'提示',
					content: '确认取消关注？',
					success(res) {
						page.$core.post({url: 'xilumarket.forum/toggle_focus',data: {user_id: userId},success: ret => {
								page.userLists.splice(index,1);
								page.$forceUpdate();
							},fail: err => {
								console.log(err);
							}
						});
					}
				})
				
			},
        }
    }
</script>

<style lang="scss" scoped>
    .xilu {
        &_avatar {
            width: 100rpx;
            height: 100rpx;
            border-radius: 50%;
            margin-right: 30rpx;
        }
    }
	.container{
		min-height: auto;
		height: auto;
	}
</style>
